﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.photos
{
    public partial class photo_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.photos().Exists(this.id))
                {
                    JscriptMsg("内容不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                GetCaizhi();
                GetWenli();
                GetYongtu();
                GetYanse();
                GetProvince();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定类别=================================
        private void GetCaizhi()
        {
            BLL.caizhi bll = new BLL.caizhi();
            DataTable dt = bll.GetList(0, "", "sort_id asc,id desc").Tables[0];

            this.rblCaizhi.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();

                this.rblCaizhi.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 纹理=================================
        private void GetWenli()
        {
            BLL.wenli bll = new BLL.wenli();
            DataTable dt = bll.GetList(0, "", "sort_id asc,id desc").Tables[0];

            this.rblWenli.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();
                this.rblWenli.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 用途=================================
        private void GetYongtu()
        {
            BLL.yongtu bll = new BLL.yongtu();
            DataTable dt = bll.GetList(0, "", "sort_id asc,id desc").Tables[0];

            this.rblYongtu.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();
                this.rblYongtu.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 颜色=================================
        private void GetYanse()
        {
            BLL.yanse bll = new BLL.yanse();
            DataTable dt = bll.GetList(0, "", "sort_id asc,id desc").Tables[0];

            this.rblYanse.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();
                this.rblYanse.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定省份=================================
        private void GetProvince()
        {
            BLL.province bll = new BLL.province();
            DataTable dt = bll.GetList(0, "", "id asc").Tables[0];

            this.ddlProvince.Items.Clear();
            this.ddlProvince.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["province_name"].ToString().Trim();

                this.ddlProvince.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定城市=================================
        private void GetCity(int province_id)
        {
            BLL.city bll = new BLL.city();
            DataTable dt = bll.GetList(0, "province_id=" + province_id, "id asc").Tables[0];

            this.ddlCity.Items.Clear();
            this.ddlCity.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["city_name"].ToString().Trim();

                this.ddlCity.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定城市=================================
        private void GetDistrict(int city_id)
        {
            BLL.district bll = new BLL.district();
            DataTable dt = bll.GetList(0, "city_id=" + city_id, "id asc").Tables[0];

            this.ddlDistrict.Items.Clear();
            this.ddlDistrict.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["district_name"].ToString().Trim();

                this.ddlDistrict.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.photos bll = new BLL.photos();
            Model.photos model = bll.GetModel(_id);

            rblStatus.SelectedValue = model.status.ToString();
            txtTitle.Text = model.title.ToString();
            //不是相册图片就绑定
            string filename = model.img_url.Substring(model.img_url.LastIndexOf("/") + 1);
            if (!filename.StartsWith("thumb_"))
            {
                txtImgUrl.Text = model.img_url;
            }
            rblCaizhi.SelectedValue = model.caizhi_id.ToString();
            rblWenli.SelectedValue = model.wenli_id.ToString();
            rblYongtu.SelectedValue = model.yongtu_id.ToString();
            rblYanse.SelectedValue = model.yanse_id.ToString();
            txtContent.Value = model.content;
            txtSeoKeywords.Text = model.keywords;
            txtSeoDescription.Text = model.description;

            ddlProvince.SelectedValue = model.province_id.ToString();
            if (model.province_id > 0)
            {
                GetCity(model.province_id);
            }
            ddlCity.SelectedValue = model.city_id.ToString();
            if (model.city_id > 0)
            {
                GetDistrict(model.city_id);
            }
            ddlDistrict.SelectedValue = model.district_id.ToString();

            txtCompanyID.Text = model.company_id.ToString();
            if (model.company_id > 0)
            {
                lbCompanyName.Text = new BLL.company().GetTitle(model.company_id);
            }

            //绑定图片相册
            if (filename.StartsWith("thumb_"))
            {
                hidFocusPhoto.Value = model.img_url; //封面图片
            }
            rptAlbumList.DataSource = model.albums;
            rptAlbumList.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            try
            {
                Model.photos model = new Model.photos();
                BLL.photos bll = new BLL.photos();

                model.title = txtTitle.Text.Trim();
                model.img_url = txtImgUrl.Text.Trim();
                model.caizhi_id = Utils.StrToInt(rblCaizhi.SelectedValue, 0);
                model.wenli_id = Utils.StrToInt(rblWenli.SelectedValue, 0);
                model.yongtu_id = Utils.StrToInt(rblYongtu.SelectedValue, 0);
                model.yanse_id = Utils.StrToInt(rblYanse.SelectedValue, 0);
                model.content = txtContent.Value;
                model.keywords = txtSeoKeywords.Text;
                model.description = txtSeoDescription.Text;

                model.province_id = Utils.StrToInt(ddlProvince.SelectedValue, 0);
                model.city_id = Utils.StrToInt(ddlCity.SelectedValue, 0);
                model.district_id = Utils.StrToInt(ddlDistrict.SelectedValue, 0);
                model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);
                model.add_time = DateTime.Now;
                model.company_id = Utils.StrToInt(txtCompanyID.Text, 0);

                #region 保存相册====================
                //检查是否有自定义图片
                if (txtImgUrl.Text.Trim() == "")
                {
                    model.img_url = hidFocusPhoto.Value;
                }
                string[] albumArr = Request.Form.GetValues("hid_photo_name");
                if (albumArr != null && albumArr.Length > 0)
                {
                    List<Model.photo_albums> ls = new List<Model.photo_albums>();
                    for (int i = 0; i < albumArr.Length; i++)
                    {
                        string[] imgArr = albumArr[i].Split('|');
                        if (imgArr.Length == 3)
                        {

                            ls.Add(new Model.photo_albums { original_path = imgArr[1], thumb_path = imgArr[2] });
                        }
                    }
                    model.albums = ls;
                }
                #endregion

                if (bll.Add(model) > 0)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            try
            {
                BLL.photos bll = new BLL.photos();
                Model.photos model = bll.GetModel(_id);

                model.title = txtTitle.Text.Trim();
                model.img_url = txtImgUrl.Text.Trim();
                model.caizhi_id = Utils.StrToInt(rblCaizhi.SelectedValue, 0);
                model.wenli_id = Utils.StrToInt(rblWenli.SelectedValue, 0);
                model.yongtu_id = Utils.StrToInt(rblYongtu.SelectedValue, 0);
                model.yanse_id = Utils.StrToInt(rblYanse.SelectedValue, 0);
                model.content = txtContent.Value;
                model.keywords = txtSeoKeywords.Text;
                model.description = txtSeoDescription.Text;

                model.province_id = Utils.StrToInt(ddlProvince.SelectedValue, 0);
                model.city_id = Utils.StrToInt(ddlCity.SelectedValue, 0);
                model.district_id = Utils.StrToInt(ddlDistrict.SelectedValue, 0);
                model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);
                model.company_id = Utils.StrToInt(txtCompanyID.Text, 0);

                #region 保存相册====================
                //检查是否有自定义图片
                if (txtImgUrl.Text.Trim() == "")
                {
                    model.img_url = hidFocusPhoto.Value;
                }
                if (model.albums != null)
                {
                    model.albums.Clear();
                }
                string[] albumArr = Request.Form.GetValues("hid_photo_name");
                if (albumArr != null)
                {
                    List<Model.photo_albums> ls = new List<Model.photo_albums>();
                    for (int i = 0; i < albumArr.Length; i++)
                    {
                        string[] imgArr = albumArr[i].Split('|');
                        int img_id = Utils.StrToInt(imgArr[0], 0);
                        if (imgArr.Length == 3)
                        {
                            ls.Add(new Model.photo_albums { id = img_id, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2] });
                        }
                    }
                    model.albums = ls;
                }
                #endregion

                if (bll.Update(model))
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
        #endregion

        //保存类别
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {

                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改内容成功！", "photo_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加内容成功！", "photo_list.aspx");
            }
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            int province_id = int.Parse(ddlProvince.SelectedValue);
            GetCity(province_id);
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            int city_id = int.Parse(ddlCity.SelectedValue);
            GetDistrict(city_id);
        }
        public string GetImgUrl(string _url)
        {
            string flag = "";
            if (_url.Contains("shicaiquanzi"))
            {
                if (_url.Contains("http://"))
                {
                    flag = _url;
                }
                else { flag = "http://" + _url; }
            }
            else
            {
                flag = "http://pic.shicaiquanzi.com" + _url;
            }
            return flag;
        }
    }
}